package com.hotacorp.opencloud.flowservice.controller;

import java.io.InputStream;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hotacorp.opencloud.common.commdata.Result;
import com.hotacorp.opencloud.common.log.annotation.LogAnnotation;
import com.hotacorp.opencloud.common.mybatisplus.PageData;
import com.hotacorp.opencloud.flowservice.dto.DeployDetailInfo;
import com.hotacorp.opencloud.flowservice.dto.DeployReq;
import com.hotacorp.opencloud.flowservice.service.DeploymentHandler;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 流程发布管理
 * @author lwg
 *
 */
@RestController
@Api(tags = "流程发布管理API")
public class DeploymentController extends AbstractController {
	@Autowired
	private DeploymentHandler deploymentHandler;

    @GetMapping(value = "/deployment/list")
    @PreAuthorize("hasAuthority('flowservice:deployment:list')")
	@ApiOperation(value = "查询发布")
    @LogAnnotation(module = "流程")
    public Result<?> list(@RequestParam Map<String, Object> params) throws Exception{
    	params.put("tenantid", String.valueOf(getTenantid()));
        PageData page = deploymentHandler.query(params);

        return Result.succeed("succeed", page);
    }
	
	@PostMapping(value = "/deployment/deploy")
    @PreAuthorize("hasAuthority('flowservice:deployment:save')")
	@ApiOperation(value = "发布流程")
    @LogAnnotation(module = "流程")
	public Result<?> deploy(@RequestBody DeployReq request) throws Exception {
		String tenantid = String.valueOf(getTenantid());
		deploymentHandler.create(tenantid, request);
		
		return Result.succeed();
	}

    @PostMapping(value = "/deployment/delete")
    @PreAuthorize("hasAuthority('flowservice:deployment:delete')")
	@ApiOperation(value = "删除发布")
    @LogAnnotation(module = "流程")
    public Result<?> delete(@RequestBody String[] ids) throws Exception{
    	deploymentHandler.delete(String.valueOf(getTenantid()), ids);
        return Result.succeed();
    }

    @GetMapping(value = "/deployment/view/{deploymentId}", produces="application/xml;charset=UTF-8")
    @PreAuthorize("hasAuthority('flowservice:deployment:view')")
	@ApiOperation(value = "查看模型")
    @LogAnnotation(module = "流程")
    public ResponseEntity<byte[]> viewDeploy(@PathVariable("deploymentId") String deploymentId) throws Exception{
    	//return deploymentHandler.getBpmn(deploymentId);
    	byte[] body = null;
        InputStream is = deploymentHandler.getBpmn(String.valueOf(getTenantid()), deploymentId);
        body = new byte[is.available()];
        is.read(body);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attchement;filename=aaa.bpmn");
        HttpStatus statusCode = HttpStatus.OK;
        ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
        return entity;
    }
    
    @GetMapping(value = "/deployment/info/{deploymentId}")
    @PreAuthorize("hasAuthority('flowservice:deployment:info')")
	@ApiOperation(value = "查看详情")
    @LogAnnotation(module = "流程")
    public Result<?> info(@PathVariable("deploymentId") String deploymentId) throws Exception {
    	DeployDetailInfo info = deploymentHandler.getDeployBpmn(String.valueOf(getTenantid()), deploymentId);
    	if (info != null) {
    		return Result.succeed("succeed", info);
    	} else {
    		return Result.failed("没有找到此发布");
    	}
    }

    @GetMapping(value = "/procdef/list")
    @PreAuthorize("hasAuthority('flowservice:procdef:list')")
	@ApiOperation(value = "查询流程")
    @LogAnnotation(module = "流程")
    public Result<?> listproc(@RequestParam Map<String, Object> params) throws Exception{
    	params.put("tenantid", String.valueOf(getTenantid()));
        PageData page = deploymentHandler.queryProcDef(params);

        return Result.succeed("succeed", page);
    }
    
    @PostMapping(value = "/procdef/delete")
    @PreAuthorize("hasAuthority('flowservice:procdef:delete')")
	@ApiOperation(value = "删除流程")
    @LogAnnotation(module = "流程")
    public Result<?> deleteproc(@RequestBody String[] ids) throws Exception{
    	deploymentHandler.deleteProcDef(String.valueOf(getTenantid()), ids);
        return Result.succeed();
    }
    
    @PostMapping(value = "/procdef/suspend")
    @PreAuthorize("hasAuthority('flowservice:procdef:suspend')")
	@ApiOperation(value = "挂起流程")
    @LogAnnotation(module = "流程")
    public Result<?> suspendproc(@RequestBody String[] ids) throws Exception{
    	deploymentHandler.suspendProcDef(String.valueOf(getTenantid()), ids);
        return Result.succeed();
    }

    @PostMapping(value = "/procdef/activate")
    @PreAuthorize("hasAuthority('flowservice:procdef:activate')")
	@ApiOperation(value = "激活流程")
    @LogAnnotation(module = "流程")
    public Result<?> activateproc(@RequestBody String[] ids) throws Exception{
    	deploymentHandler.activatesProcDef(String.valueOf(getTenantid()), ids);
        return Result.succeed();
    }

    @GetMapping(value = "/procdef/view/{processDefinitionId}", produces="application/xml;charset=UTF-8")
    @PreAuthorize("hasAuthority('flowservice:procdef:view')")
	@ApiOperation(value = "查看流程")
    @LogAnnotation(module = "流程")
    public ResponseEntity<byte[]> viewProcDefBpmn(@PathVariable("processDefinitionId") String processDefinitionId) throws Exception{
    	//return deploymentHandler.getBpmn(deploymentId);
    	byte[] body = null;
        InputStream is = deploymentHandler.getProcDefBpmn(String.valueOf(getTenantid()), processDefinitionId);
        body = new byte[is.available()];
        is.read(body);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attchement;filename=aaa.bpmn");
        HttpStatus statusCode = HttpStatus.OK;
        ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
        return entity;
    }

}
